# The planner's problem with infinite horizon
# unconstrained solution

###############   Declaration of Parameters ###############################
#-- Households' parameters
param BETA;
param SIGMA;
param CHI;      # entrepreneurs'population share
param NU;
param MU;

#-- Firms' parameters
param A;        # aggregate productivity (for generality)
param ALPHA;    # capital share
param DELTA;    # depreciation rate (set to 1 in the simple model)

#-- Financial market parameters
param eta_0; 
param eta_1; 
   
#-- Gov' Parameters
param G_TARGET; # Note: when solving G, there is no G_TARGET;
param ZETA_SS;


###############   Declaration of Variables ################################
var C >= 0;
var L >= 0;
var K >= 0;
var B >= 0;
var phi >=0, <=1;
var lambda_Psi >=0; # Lagrange multiplier normalized by Psi
var gamma_Psi  >=0; # Lagrange multiplier normalized by Psi
var zeta >=1,  <= 1/ BETA;
var G >=0;

var q_w >= 1;
var q   >= 1;
var eta >= 0;
var rho >= 0;

var MU_L;
var u_p_C;
var u_pp_C;
var eta_p;
var eta_pp;
var rho_p;
var phi_eta_p;
var inv_CHI_rho_p;
var q_w_p;

#-- prices to be solved in the .run file
var tau_k;
var tau_l; 
var p_b;

#################### The Planner's Constraints #############################
subject to soc:
C + G + (1 + phi * eta) * K = A * K^ALPHA * L^(1 - ALPHA) + (1 + (1 - CHI) * phi * eta) * (1 - DELTA) * K;

subject to fin_constr:
CHI * B / BETA  + (1 + CHI * rho) * ((1 - (1 - CHI) * phi * q) * (1 - DELTA) - (1 - phi * q)) * K = 0;

subject to implementability:
u_p_C * (C + B + q_w * K) = MU_L * L + u_p_C * (B + q_w * K) / BETA;

subject to foc_capital:
lambda_Psi * (1 + phi * eta) + u_p_C * q_w * (zeta - 1) + gamma_Psi * u_p_C * (1 - phi * q)
= BETA * lambda_Psi * zeta * (A * ALPHA * K^(ALPHA - 1) * L^(1 - ALPHA) + (1 + (1 - CHI) * phi * eta) * (1 - DELTA))
+ BETA * gamma_Psi * zeta * u_p_C * (1 - (1 - CHI) * phi * q) * (1 - DELTA);

subject to foc_bond:
#zeta * (1 - CHI * gamma_Psi) = 1 + CHI * rho;
#zeta = 1;
CHI * zeta * gamma_Psi = (1 + CHI * rho) * (zeta - 1);

subject to foc_consumption:
u_p_C + u_pp_C * (C + B + q_w * K - gamma_Psi * CHI * B / (1 + CHI * rho) * (1 - zeta * BETA) / BETA)
= lambda_Psi + u_pp_C * zeta * (q_w * K + B);

subject to foc_labor:
#L = (A * (1 - ALPHA) / MU / (1 + NU) * lambda_Psi)^(1 / (ALPHA + NU)) * K^(ALPHA / (ALPHA + NU));
MU * L^NU + MU * NU * L^NU = lambda_Psi * A * (1 - ALPHA) * K^ALPHA * L^(-ALPHA);

subject to foc_asset_liquidity:
u_p_C * gamma_Psi * CHI / BETA  * B  * inv_CHI_rho_p  
+ u_p_C * gamma_Psi * (1 - (1 - CHI) * (1 - DELTA)) * (q + phi * (q_w_p - eta_p)) * K
- lambda_Psi  * (1 - (1 - CHI) * (1 - DELTA)) * phi_eta_p * K
= u_p_C * (zeta - 1) * K * q_w_p;

#----------------------- definition of derivatives
subject to def_u_p_C:
u_p_C = C^(-SIGMA);

subject to def_u_pp_C:
u_pp_C = (-SIGMA) * C^(-SIGMA - 1);

subject to def_v_p_L:
MU_L = MU * L^NU;

subject to def_eta:
eta = eta_0 * phi^eta_1;

subject to def_eta_p:
eta_p = eta_0 * eta_1 * phi^(eta_1 - 1);

subject to def_q:
q_w = 1 + eta + (1 - phi) * phi * eta_p;

subject to def_q_w:
q = q_w - eta;

subject to def_rho:
#rho = (1 - phi) * q_w / (1 - phi * q) - 1;
rho * (1 - phi^2 * eta_p) = eta + phi * eta_p;

subject to def_eta_pp:
eta_pp = eta_0 * eta_1 * (eta_1 - 1) * phi^(eta_1 - 2);

subject to def_q_w_p:
q_w_p = (1 - phi) * (2 * eta_p + phi * eta_pp);

subject to def_rho_p:
rho_p * (1 - phi^2 * eta_p)^2 = (2 * eta_p + phi * eta_pp) * (1 + phi * eta);

subject to def_phi_eta_p:
phi_eta_p = eta + phi * eta_p;

subject to def_inv_CHI_rho_p:
inv_CHI_rho_p * (1 + CHI * rho)^2 = - CHI  * rho_p;

############# Declaration of Policy Implementation ########################

#back out taxes and interest rates
subject to capital_tax:
#### with depreciation allowance
q_w = BETA * ((1 - tau_k) * ALPHA * A * K^(ALPHA - 1) * L^(1 - ALPHA) + (1 - DELTA * (1 - tau_k)) * q_w + CHI * (1 - DELTA) * phi * (q * (1 + rho) - q_w));

subject to labor_tax:
(1 - tau_l) * A * (1 - ALPHA) * K^ALPHA * L^(-ALPHA) * u_p_C = MU_L;

subject to interest_rate:
p_b = BETA  * (1 + CHI * rho);

subject to knowing_government_expenditures:
G = G_TARGET;

subject to knowing_implementability_tightness:
zeta = ZETA_SS;